package com.authine.cloudpivot.ext.vo;

import com.authine.cloudpivot.ext.utils.RegexUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

@Data
@Slf4j
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ApiModel("扩展字段vo，用于商品相关的其它字段")
public class OrderGoodsExtendsAttrVO {

    @ApiModelProperty("送货要求")
    private String deliveryRequirements;
    @ApiModelProperty("对接外部工厂代码")
    private String clientFactoryCode;
    @ApiModelProperty("申购人")
    private String requisitioner;
    @ApiModelProperty("补充澄清")
    private String clarifyTips;

    public static String writeValueAsString(String deliveryRequirements) {
        if (!RegexUtils.notNull(deliveryRequirements)) {
            return null;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(
                    OrderGoodsExtendsAttrVO.builder()
                            .deliveryRequirements(deliveryRequirements)
                            .build()
            );
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static String writeValueAsString(OrderGoodsExtendsAttrVO extendsAttrVO) {
        if (null == extendsAttrVO) {
            return null;
        }

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(extendsAttrVO);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static OrderGoodsExtendsAttrVO readValue(String thisValue) {
        if (!RegexUtils.notNull(thisValue)) {
            return OrderGoodsExtendsAttrVO.builder().build();
        }

        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(thisValue, OrderGoodsExtendsAttrVO.class);
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return OrderGoodsExtendsAttrVO.builder().build();
    }
}
